package com.mmall.common;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;
@JsonSerialize()
public class ServerResponse<T> implements Serializable {

    private int status;

    private String msg;

    private T data;

    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    private ServerResponse(int status){
        this.status=status;
    }
    private ServerResponse(int status,String msg){
        this.msg=msg;
        this.status=status;
    }
    private ServerResponse(int status,T data){
        this.status=status;
        this.data=data;
    }
    private ServerResponse(int status,String msg,T data){
        this.status=status;
        this.msg=msg;
        this.data=data;
    }
    @JsonIgnore
    public   boolean checkIsSuccess(){

        return this.status==ResponseCode.SUCCESS.getStatus();
    }

    public  static <T> ServerResponse<T> createServerResponseBySuccess(){

        return new ServerResponse<T>(ResponseCode.SUCCESS.getStatus());

    }
    public static <T> ServerResponse<T> createServerResponseBySuccess(String msg){

        return new ServerResponse<T>(ResponseCode.SUCCESS.getStatus(),msg);

    }
    public static <T> ServerResponse<T> createServerResponseBySuccess(T data){

        return new ServerResponse<T>(ResponseCode.SUCCESS.getStatus(),data);

    }
    public static <T> ServerResponse<T> createServerResponseBySuccess(String msg,T data){

        return new ServerResponse<T>(ResponseCode.SUCCESS.getStatus(),msg,data);

    }
    public static <T> ServerResponse<T> createServerResponseByFailure(){

        return new ServerResponse<T>(ResponseCode.ERROR.getStatus());
    }
    public static <T> ServerResponse<T> createServerResponseByFailure(String msg){

        return new ServerResponse<T>(ResponseCode.ERROR.getStatus(),msg);

    }
    public  static <T> ServerResponse<T> createServerResponseByFailure(T data){

        return new ServerResponse<T>(ResponseCode.ERROR.getStatus(),data);

    }
    public  static <T> ServerResponse<T> createServerResponseByFailure(String msg,T data){

        return new ServerResponse<T>(ResponseCode.ERROR.getStatus(),msg,data);

    }
    public  static <T> ServerResponse<T> createServerResponseByFailure(Integer status,String msg){

        return new ServerResponse<T>(status,msg);

    }

    @Override
    public String toString() {
        return "ServerResponse{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}


